
export default class MpConnectNative {
  constructor () {
    this.instance = null
    this.reset = true
    this.bridge = wx
  }

  static getInstance () {
    console.log('MpConnectNative getInstance Name:', this.name)
    if (!this.instance) {
      this.instance = new MpConnectNative()
    }
    return this.instance
  }

  /**
   * 调起原生webview通用
   * @param {*} data
   */
  callNativeHandler (data) {
    if (!data) {
      return
    }
    if (typeof (data) === 'string') {
      this.bridge.navigateTo({
        url: data
      })
    } else if (typeof (data) === 'object') {
      const {type, route, allow, tips} = data
      if (allow) {
        const mine = mpvue.getStorageSync('mine')
        console.log('mine', mine)
        console.log('mine allew', mine[allow])
        if (allow === 'actived') {
          if (!mine || !mine.actived) {
            wx.showModal({
              title: '账号登录提示',
              content: '需要您登录后才可进行' + tips,
              confirmText: '去登录',
              success (res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '/pages/login/main'
                  })
                }
              }
            })
            return
          }
        } else if (allow === 'authented') {
          if (!mine || !mine.actived) {
            wx.showModal({
              title: '账号登录提示',
              content: '需要您登录后才可进行' + tips,
              confirmText: '去登录',
              success (res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '/pages/login/main'
                  })
                }
              }
            })
            return
          } else if (!mine.authented) {
            wx.showModal({
              title: '用户资料完善提示',
              content: '需要您完善个人资料才可进行' + tips,
              confirmText: '去完善',
              success (res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '/pages/authent/main'
                  })
                }
              }
            })
            return
          }
        }
      }
      if (type === 'push') {
        this.bridge.navigateTo({
          url: route
        })
      } else if (type === 'replace') {
        this.bridge.redirectTo({
          url: route
        })
      } else if (type === 'back') {
        this.bridge.navigateBack({
          delta: route
        })
      } else {
        wx.reLaunch({
          url: route
        })
      }
    }
  }
}
